//
//  S1mpl3FocusCollectionViewCell.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "S1mpl3FocusCollectionViewCell.h"
#import "FitHeader.h"
#import "UIImageView+WebCache.h"
@interface S1mpl3FocusCollectionViewCell ()<UIScrollViewDelegate>
@property(nonatomic,assign)NSInteger num;
@end

@implementation S1mpl3FocusCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * FITWidth, 200 * FITHeight)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jump)];
        [_scrollView addGestureRecognizer:tap];
        [self.contentView addSubview:_scrollView];
    }
    return self;
}

-(void)setDataSourceArray:(NSMutableArray *)dataSourceArray{
    _scrollView.contentSize = CGSizeMake(375 * FITWidth * (dataSourceArray.count + 2), 200 * FITHeight);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.userInteractionEnabled = YES;
    _scrollView.contentOffset = CGPointMake(375 * FITWidth, 0);
    _num = dataSourceArray.count;
    for (NSInteger i = 0; i < dataSourceArray.count + 2; i++) {
        if (i < 1) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375 * FITWidth, 200 * FITHeight)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[[dataSourceArray lastObject] pic]] placeholderImage:[UIImage imageNamed:@""]];
            [_scrollView addSubview:imageView];
        }else if (i >= 1 && i < _num + 1){
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(375 * FITWidth * i, 0, 375 * FITWidth , 200 * FITHeight)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[[dataSourceArray objectAtIndex:i - 1] pic]] placeholderImage:[UIImage imageNamed:@""]];
            [_scrollView addSubview:imageView];

        }else{
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(375 * FITWidth * i, 0, 375 * FITWidth, 200 * FITHeight)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[[dataSourceArray firstObject] pic]] placeholderImage:[UIImage imageNamed:@""]];
            [_scrollView addSubview:imageView];

        }
    }
    _scrollView.delegate = self;

}

-(void)jump{
    [self.delegate sendMessage:[_dataSourceArray objectAtIndex:_scrollView.contentOffset.x / (375 * FITWidth)]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
